/**
 * \class CompoundContext.cpp
 *
 *  Created on: 15 apr 2011
 *      Author: ytc0039
 */

#include "CompoundContext.h"
#include <AbsSyn/GenExpClasses/ExpressionType.h>

CompoundContext::CompoundContext(IContext *context):IContextDecorator(context) {

}

CompoundContext::~CompoundContext() {

}

bool CompoundContext::identExists(const Ident & ident) const
{

	bool ret = IContextDecorator::identExists(ident);

	if( !ret )
	{
		//check if it exists in this context.
		if(m_context.find(ident) != m_context.end() )
		{
			ret = true;
		}

	}
	return ret;

}

bool CompoundContext::removeIdent(const Ident & ident)
{
	//Remove it from the parent context
	bool ret = IContextDecorator::removeIdent(ident);

	//remove from this context.
	CONTEXT_MAP::iterator it = m_context.find(ident);

	if(it != m_context.end() )
	{
		m_context.erase (it);
		ret = true;
	}


	return ret;
}


bool CompoundContext::findType(const Ident& ident,TypeEnum& type)const
{
	bool ret = false;

	//Check if its in the above context, if not check in the current context.
	if( !IContextDecorator::findType(ident,type) )
	{
		CONTEXT_MAP::const_iterator it = m_context.find(ident);

		if( it != m_context.end() )
		{
			type = it->second;
			ret = true;
		}
	}


	return ret;

}



bool CompoundContext::addIdent(TypeEnum type, const Ident & ident)
{
	bool ret = false;
	//here we add the ident to this context.
	// though we need to check that its not part of any other context.

	if( !identExists(ident) ) //check that it doesn't exists
	{
		std::pair< CONTEXT_MAP::iterator , bool > insRet = m_context.insert(CONTEXT_MAP::value_type(ident,type));
		ret = insRet.second;
	}
	return ret;
}

void CompoundContext::getAllOfType(TypeEnum type, IContext::TypeList & list) const
{
	//Checks all the previous contexts if existing there.
	IContextDecorator::getAllOfType(type,list);

	CONTEXT_MAP::const_iterator iter = m_context.begin();
	for(;iter != m_context.end(); ++iter)
	{
		TypeEnum mapType = (*iter).second;

		if( type == mapType)
		{
			list.push_back(iter->first);
		}
	}
}







